﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.AuthorService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 用户登录记录
    /// </summary>
    public class UserLandingRecordsViewModel : ShowListViewModelBase<AuthorLandingRecordExport, AuthorServiceClient>
    {
        DateTime? beginTime = null;
        DateTime? endTime = null;
        string keyword;
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime
        {
            get
            {
                return beginTime;
            }
            set
            {
                if (beginTime != value)
                {
                    beginTime = value;
                    OnPropertyChanged("BeginTime");
                    Refresh(null);
                }
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                if (endTime != value)
                {
                    endTime = value;
                    OnPropertyChanged("EndTime");
                    Refresh(null);
                }
            }
        }

        public string Keyword
        {
            get { return keyword; }
            set
            {
                if (keyword == value) { return; }
                keyword = value == "" ? null : value;
                OnPropertyChanged("Keyword");
                Refresh(null);
            }
        }

        public UserLandingRecordsViewModel()
            : base(PageCategory.用户管理.ToString(), Page.用户登录记录.ToString())
        {
            client.GetLandingRecordsCompleted += this.WrapEventHandler<GetLandingRecordsCompletedEventArgs>(GetLandingRecordsCompleted);
        }

        void GetLandingRecordsCompleted(object sender, GetLandingRecordsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }
 
        protected override void Refresh(object obj)
        {
            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            client.GetLandingRecordsAsync(new GetLandingRecordsImport() {
                BeginTime = this.BeginTime,
                EndTime = this.EndTime,
                PageIndex = pageIndex,
                Keyword = this.Keyword,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
      
        }

        protected override void Reset(object obj)
        {
            this.PageIndex = 1;
            Refresh(null);
        }
    }
}
